/*
 * FormCustomizer.java
 *
 * Created on January 23, 2009, 4:55 PM
 */

package org.netbeans.modules.web.stripes.palette.items;

import javax.swing.text.JTextComponent;

import org.netbeans.modules.web.stripes.palette.CodeSnippet;
import org.netbeans.modules.web.stripes.palette.CodeSnippetCustomizer;
import org.netbeans.modules.web.stripes.palette.StripesPaletteUtilities;
import org.netbeans.modules.web.stripes.util.StringUtils;
import org.netbeans.modules.web.stripes.util.browse.BrowseDialogs;

/**
 *
 * @author  Josef Sustacek
 */
public class FormCustomizer extends CodeSnippetCustomizer {

    private JTextComponent target;
    
    /** Creates new form FormCustomizer */
    public FormCustomizer(CodeSnippet codeSnippet, JTextComponent target) {
        super(codeSnippet);
        
        this.target = target;
        
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        buttonGroup = new javax.swing.ButtonGroup();
        actionText = new javax.swing.JTextField();
        actionButton = new javax.swing.JRadioButton();
        beanclassButton = new javax.swing.JRadioButton();
        beanclassText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        methodCombo = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        actionText.setText(org.openide.util.NbBundle.getMessage(FormCustomizer.class, "FormCustomizer.actionText.text")); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, actionButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), actionText, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonGroup.add(actionButton);
        actionButton.setSelected(true);
        actionButton.setText(org.openide.util.NbBundle.getMessage(FormCustomizer.class, "FormCustomizer.actionButton.text")); // NOI18N

        buttonGroup.add(beanclassButton);
        beanclassButton.setText(org.openide.util.NbBundle.getMessage(FormCustomizer.class, "FormCustomizer.beanclassButton.text")); // NOI18N

        beanclassText.setText(org.openide.util.NbBundle.getMessage(FormCustomizer.class, "FormCustomizer.beanclassText.text")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, beanclassButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), beanclassText, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel2.setText(org.openide.util.NbBundle.getMessage(FormCustomizer.class, "FormCustomizer.jLabel2.text")); // NOI18N

        methodCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "post", "get" }));

        jButton1.setText(org.openide.util.NbBundle.getMessage(FormCustomizer.class, "FormCustomizer.jButton1.text")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, beanclassButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), jButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(beanclassButton)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(beanclassText, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(actionText, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(methodCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(actionButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actionButton)
                    .addComponent(actionText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(beanclassButton)
                    .addComponent(beanclassText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(methodCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String beanFQN = BrowseDialogs.javaSourcesDialog(
            StripesPaletteUtilities.getProject(target), this);
    
    if(!StringUtils.nullOrEmpty(beanFQN)){
        beanclassText.setText(beanFQN);
    }    
}//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton actionButton;
    private javax.swing.JTextField actionText;
    private javax.swing.JRadioButton beanclassButton;
    private javax.swing.JTextField beanclassText;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox methodCombo;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    protected void evaluateInput() {
        if(actionButton.isSelected()){
            codeSnippet.setProperty(Form.ACTION, actionText.getText());
            codeSnippet.setProperty(Form.BEAN_CLASS, null);
        }
        
        if(beanclassButton.isSelected()){
            codeSnippet.setProperty(Form.BEAN_CLASS, beanclassText.getText());
            codeSnippet.setProperty(Form.ACTION, null);
        }
        
        codeSnippet.setProperty(Form.METHOD, methodCombo.getSelectedItem().toString());
    }
}
